package com.live.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.live.adapter.LiveSearchSA;
import com.live.common.Constant;
import com.live.common.LiveCommon;
import com.live.task.LiveSearch;
import com.live.tools.LiveTools;

public class LiveSearchActivity extends LiveBaseActivity {

	private ListView matchLV = null;
	private LiveSearchSA searchSA = null;
	private List<Map<String, Object>> matchList = new ArrayList<Map<String, Object>>();

	private String searchKeyStr = "";
	private View liveFoot;
	private int page = 0;
	private int lastIndex = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_search);

		// 设置头部信息
		LiveCommon.setHead(context, (LinearLayout) findViewById(R.id.liveHead), R.string.title_match_search,
				R.string.home_search);

		// 设置头部按钮（发表）
		Button topicBtn = (Button) findViewById(R.id.headFuncBtn);
		topicBtn.setVisibility(Constant.INVISIBLE);

		/* 获取Intent中的Bundle对象 */
		Bundle bundle = this.getIntent().getExtras();
		searchKeyStr = bundle.getString("searchKeyStr");

		liveFoot = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.live_foot, null, false);

		searchSA = new LiveSearchSA(context, matchList, R.layout.search_list,
				new String[] { "matchName", "matchTime", "homeTeam", "homeTeamScore", "visitTeam", "visitTeamScore",
						"matchStatus", "matchRecord", "matchWatch" }, new int[] { R.id.matchName, R.id.matchTime,
						R.id.homeTeam, R.id.homeTeamScore, R.id.visitTeam, R.id.visitTeamScore, R.id.matchStatus,
						R.id.matchRecord, R.id.matchWatch });

		matchLV = (ListView) findViewById(R.id.matchList);
		matchLV.addFooterView(liveFoot);
		matchLV.setAdapter(searchSA);
		matchLV.setOnItemClickListener(new OnItemClickListener() {

			// 从当前画面到比分明细画面跳转，并传值。
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Map<String, Object> sportsMatch = matchList.get(arg2);

				Intent intent = new Intent();

				intent.setClass(context, LiveDetailActivity.class);

				// 实例化 Bundle，设置需要传递的参数
				Bundle bundle = new Bundle();
				bundle.putString("matchId", sportsMatch.get("matchId").toString());
				bundle.putString("matchOptaId", sportsMatch.get("matchOptaId").toString());
				bundle.putString("matchName", sportsMatch.get("matchName").toString());
				if (sportsMatch.get("matchRecord") != null) {
					bundle.putString("matchRecord", sportsMatch.get("matchRecord").toString());
				}
				bundle.putString("matchCategory", sportsMatch.get("matchCategory").toString());
				bundle.putString("matchTime", sportsMatch.get("matchTime").toString());
				bundle.putString("homeTeam", sportsMatch.get("homeTeam").toString());
				bundle.putString("homeTeamName", sportsMatch.get("homeTeamName").toString());
				bundle.putString("homeTeamScore", sportsMatch.get("homeTeamScore").toString());
				bundle.putString("homeTeamPic", sportsMatch.get("homeTeamPic").toString());
				bundle.putString("visitTeam", sportsMatch.get("visitTeam").toString());
				bundle.putString("visitTeamName", sportsMatch.get("visitTeamName").toString());
				bundle.putString("visitTeamScore", sportsMatch.get("visitTeamScore").toString());
				bundle.putString("visitTeamPic", sportsMatch.get("visitTeamPic").toString());
				bundle.putString("matchStatus", sportsMatch.get("matchStatus").toString());
				bundle.putString("matchWatch", sportsMatch.get("matchWatch").toString());

				intent.putExtras(bundle);

				context.startActivity(intent);
			}
		});

		matchLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && lastIndex == searchSA.getCount()) {
					page = page + 1;
					Bundle bundle = new Bundle();
					bundle.putInt("page", page);
					String url = Constant.SITE_URL + "live_search.php?locale=" + locale + "&key=" + searchKeyStr
							+ "&keySimp=" + LiveTools.simpToTrad(searchKeyStr, locale) + "&page=" + page;
					LiveSearch searchTask = new LiveSearch(context, searchSA, matchList, matchLV, liveFoot);
					searchTask.execute(url);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				lastIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});

		String url = Constant.SITE_URL + "live_search.php?locale=" + locale + "&key=" + searchKeyStr + "&keySimp="
				+ LiveTools.simpToTrad(searchKeyStr, locale) + "&page=" + page;
		LiveSearch searchTask = new LiveSearch(context, searchSA, matchList, matchLV, liveFoot);
		searchTask.execute(url);
	}
}